﻿/* $RCSFile: CurrencyDataType.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/UI/DataTypes/CurrencyDataType.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:29  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.ComponentModel;
using System.Configuration;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Globalization;

using AddonNice.UI;
using AddonNice.UI.DataTypes;
using AddonNice.UI.WebControls;
using AddonNice.Configuration;
using AddonNice.Diagnostics;


namespace AddonNice.UI.DataTypes
{
    public class CurrencyEventArg : EventArgs
    {
        public CurrencyPricingChooser CPChooser    =   null;
        public CurrencyEventArg(CurrencyPricingChooser p)
        {
            CPChooser   =   p;
        }
    }

    public class CurrencyDataType : BaseDataType
    {
        int CurrencyIndex   =   0;            
        public event NeedDataEventHandler NeedDataEvent;

        public CurrencyDataType(bool ask4Data,bool addEmptyItem,int currencyIndex)
        {
            Ask4Data        =   ask4Data;
            AddEmptyItem    =   addEmptyItem;
            CurrencyIndex   =   currencyIndex;
            InnerDataType   =   PropertiesDataType.List;
        }


        bool _Ask4Data  =   false;
        public bool Ask4Data
        {
            get
            {
                return _Ask4Data;
            }
            set
            {
                _Ask4Data   =   value;
            }
        }
        public bool AddEmptyItem    =   false;

        public void OnNeedData(EventArgs ev)
        {
		    if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CurrencyDataType OnNeedData BEG NeedDataEvent null: {0}", ( NeedDataEvent == null )),DataTypeTraceSwitch.Sw.Info);
            if ( NeedDataEvent != null )
                NeedDataEvent(this,ev);
		    if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( "[V]CurrencyDataType OnNeedData END.",DataTypeTraceSwitch.Sw.Info);
        }

        protected override void InitializeComponents()
        {
            CurrencyPricingChooser dd       =   new CurrencyPricingChooser(!Ask4Data,AddEmptyItem,CurrencyIndex);
            dd.Width                        =   controlWidth;
   		    if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CurrencyDataType InitializeComponents HandleChanges: {0}",HandleChanges),DataTypeTraceSwitch.Sw.Info);
            /*
            if ( HandleChanges )
            {
                dd.AutoPostBack         =   true;
                dd.SelectedIndexChanged +=  new RadComboBoxSelectedIndexChangedEventHandler(dd_SelectedIndexChanged);
            }
             * */
            innerControl                = dd;
        }

        /*
        void dd_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
    		if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CurrencyDataType dd_SelectedIndexChanged sender: {0}",sender),DataTypeTraceSwitch.Sw.Info);
            string oldValue         =   Val;
            ReadDataFromEditControl();
            string newValue         =   Val;
            OnDataTypeEvent(new DataTypeEventArgs(DataTypeEventType.Changed,sender,e,oldValue,newValue));
        }
         * */
       
        public override void DataBind()
        {
            if (DataTypeTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CurrencyDataType DataBind BEG Ask4Data: {0}.", Ask4Data), DataTypeTraceSwitch.Sw.Info);
			try
			{
                CurrencyPricingChooser dd   = (CurrencyPricingChooser)innerControl;
                if ( Ask4Data )
                    OnNeedData(new CurrencyEventArg(dd));
                dd.DataBind();
                dd.SelectedItem             =   Val;
			}
			catch (Exception ex)
			{
                if ( DataTypeTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]CurrencyDataType DataBind ex: {0}",ex),DataTypeTraceSwitch.Sw.Info);
                throw;
			}
            if (DataTypeTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CurrencyDataType DataBind END Ask4Data: {0}.", Ask4Data), DataTypeTraceSwitch.Sw.Info);
        }

        public override string ReadDataFromEditControl()
		{
            Control Ctl =   EditControl;
            if (DataTypeTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CurrencyDataType ReadDataFromEditControl BEG Control is null: {0}, Val: {1}.", (Ctl == null), Val ), DataTypeTraceSwitch.Sw.Info);
            if ( Ctl == null )
                return Val;
            if ( Ctl is CurrencyPricingChooser)
            {
                CurrencyPricingChooser dd       =   (CurrencyPricingChooser)Ctl;
                innerValue                      =   dd.SelectedItem;
            }
            if (DataTypeTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CurrencyDataType ReadDataFromEditControl END InnerValue: {0}.", innerValue), DataTypeTraceSwitch.Sw.Info);
            return Val;
        }

        public override Control EditControl
        {
            get
            {
                if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( "[V]CurrencyDataType EditControl GET.",DataTypeTraceSwitch.Sw.Info);
                if (innerControl == null)
                    InitializeComponents();
                return innerControl;
            }
            set
            {
                if (value is CurrencyPricingChooser)
                {
                    if ( value != innerControl )
                        innerControl            =   value;
                    CurrencyPricingChooser dd   =   (CurrencyPricingChooser)innerControl;
                    /*
                    if ( HandleChanges )
                    {
                        dd.AutoPostBack         =   true;
                        dd.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(dd_SelectedIndexChanged); 
                    }
                     * */
                    // DO NOT use Value but use InnerValue
                    if (dd.SelectedItem != null)
                            innerValue          =   dd.SelectedItem;
                    else    innerValue          =   string.Empty;
                    if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CurrencyDataType EditControl SET innerValue: '{0}'.",innerValue),DataTypeTraceSwitch.Sw.Info);
                }
                else
                {
                    string msg  =   string.Format("[E]CurrencyDataType EditControl RadCombobox values is required :{0} ", value.GetType().Name);
                    if ( DataTypeTraceSwitch.Sw.TraceError )
                        Trace.WriteLine( msg,DataTypeTraceSwitch.Sw.Info);
                    throw new ArgumentException(msg);
                }
            }
        }

		public override string Val
		{
			get
			{
				return innerValue;
			}
			set
			{
				innerValue =    value;
                if ( innerControl == null )
                    return;
				if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CurrencyDataType Value Set value: {0}", value),DataTypeTraceSwitch.Sw.Info);
				//Update value in control
				((CurrencyPricingChooser) innerControl).SelectedItem = value;
			}
		}

        public override string Description
        {
            get
            {
                return "Currency Pricing Chooser";
            }
        }
    }
}
